//
// Created on 2023/9/10.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef KComponent_plugin_render_H
#define KComponent_plugin_render_H

#include <unordered_map>

#include <ace/xcomponent/native_interface_xcomponent.h>
#include <napi/native_api.h>

#include "camera_manager.h"
#include "render_thread.h"

namespace NativeXComponentSample {
class PluginRender {
public:
    explicit PluginRender(std::string &id);
    ~PluginRender() {
        Release(id_);
    }
    static PluginRender *GetInstance(std::string &id);
    static void Release(std::string &id);
    static napi_value startCamera(napi_env env, napi_callback_info info);
    static napi_value TestGetXComponentStatus(napi_env env, napi_callback_info info);
    static napi_value NativeCallArkTS(napi_env env, napi_callback_info info);
    void Export(napi_env env, napi_value exports);
    void OnSurfaceChanged(OH_NativeXComponent *component, void *window);
    void OnTouchEvent(OH_NativeXComponent *component, void *window);
    void OnMouseEvent(OH_NativeXComponent *component, void *window);
    void OnHoverEvent(OH_NativeXComponent *component, bool isHover);
    void OnFocusEvent(OH_NativeXComponent *component, void *window);
    void OnBlurEvent(OH_NativeXComponent *component, void *window);
    void OnKeyEvent(OH_NativeXComponent *component, void *window);
    void RegisterCallback(OH_NativeXComponent *nativeXComponent);

    // @param window - NativeWindow created by XComponent
    // @param width - NativeWindow width.
    // @param height - NativeWindow height.
    void UpdateNativeWindow(void *window, uint64_t width, uint64_t height);

public:
    static std::unordered_map<std::string, PluginRender *> instance_;
    std::string id_;
    static int32_t hasDraw_;
    static int32_t hasChangeColor_;

private:
    void UpdateNativeWindowSize(uint64_t width, uint64_t height);
    void CreateCamera();

    OH_NativeXComponent_Callback renderCallback_;
    OH_NativeXComponent_MouseEvent_Callback mouseCallback_;

    std::unique_ptr<RenderThread> renderThread_;
    std::unique_ptr<OHOS_CAMERA_SAMPLE::NDKCamera> ndkCamera_;
};
} // namespace NativeXComponentSample
#endif // KComponent_plugin_render_H
